<template>
  <div
    class="modal fade"
    tabindex="-1"
    role="dialog"
    aria-hidden="true"
    :id="id"
  >
    <div class="modal-dialog modal-dialog-centered">
      <div class="modal-content">
        <div class="modal-header">
          <h4 class="modal-title">
            <slot name="title"> 新建/更新机场信息 </slot>
          </h4>
          <button
            type="button"
            class="btn-close"
            data-bs-dismiss="modal"
            aria-label="Close"
          ></button>
        </div>
        <div class="modal-body">
          <form role="form" class="form-horizontal">
            <div class="row mb-2">
              <label class="control-label col-4"> 机场三字代码 </label>
              <div class="col-8">
                <input
                  type="text"
                  class="form-control"
                  v-model.trim="threeCode"
                />
              </div>
            </div>
            <div class="row mb-2">
              <label class="control-label col-4"> 城市三字代码 </label>
              <div class="col-8">
                <input
                  type="text"
                  class="form-control"
                  v-model.trim="cityCode"
                />
              </div>
            </div>
            <div class="row mb-2">
              <label class="control-label col-4"> 城市名 </label>
              <div class="col-8">
                <input
                  type="text"
                  class="form-control"
                  v-model.trim="cityName"
                />
              </div>
            </div>
            <div class="row mb-2">
              <label class="control-label col-4"> 机场中文名 </label>
              <div class="col-8">
                <input type="text" class="form-control" v-model.trim="nameCn" />
              </div>
            </div>
            <div class="row mb-2">
              <label class="control-label col-4"> 机场英文名 </label>
              <div class="col-8">
                <input type="text" class="form-control" v-model.trim="nameEn" />
              </div>
            </div>
            <div class="row mb-2">
              <label class="control-label col-4"> 国家代码 </label>
              <div class="col-8">
                <input
                  type="text"
                  class="form-control"
                  v-model.trim="countryCode"
                />
              </div>
            </div>
          </form>
        </div>
        <div class="modal-footer">
          <button
            type="button"
            class="btn btn-secondary me-2"
            data-bs-dismiss="modal"
          >
            关闭
          </button>
          <button type="button" class="btn btn-primary" @click.stop="ok()">
            确定
          </button>
        </div>
      </div>
    </div>
  </div>
</template>


<script>
import $ from "jquery";

export default {
  props: {
    nullable: {
      type: Boolean,
      default: true,
    },
    id: {
      type: String,
      default: "airportModal",
    },
  },
  data() {
    return {
      resolve: "",
      reject: "",
      promise: "",

      threeCode: "",
      countryCode: "",
      cityCode: "",
      cityName: "",
      nameCn: "",
      nameEn: "",

      myModal: null,
    };
  },
  methods: {
    ok: function () {
      this.hideModal();

      this.resolve({
        threeCode: this.threeCode,
        cityCode: this.cityCode,
        cityName: this.cityName,
        countryCode: this.countryCode,
        airportName: this.nameCn,
        airportEnglishName: this.nameEn
      });
    },
    modal: function (v) {
      this.threeCode = v.threeCode;
      this.cityCode = v.cityCode;
      this.cityName = v.cityName;
      this.countryCode = v.countryCode;
      this.airportName = v.nameCn;
      this.airportEnglishName = v.nameEn;

      this.showModal();

      this.promise = new Promise((resolve, reject) => {
        this.resolve = resolve;
        this.reject = reject;
      });
      return this.promise;
    },
    showModal: function () {
      this.getMyModal().show();
    },
    hideModal: function () {
      this.getMyModal().hide();
    },
    getMyModal: function () {
      if (this.myModal === null) {
        let myModalEl = $(this.$el).get(0);
        this.myModal = new bootstrap.Modal(myModalEl, {
          keyboard: true,
        });
      }

      return this.myModal;
    },
  },
};
</script>
